package org.monkey.example.websocket.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.monkey.common.cbs.util.PageConvertUtil;
import org.monkey.common.constant.entity.Result;
import org.monkey.common.constant.util.DataUtil;
import org.monkey.example.websocket.entity.convert.SocketMsgConvert;
import org.monkey.example.websocket.entity.dto.SocketMsgDTO;
import org.monkey.example.websocket.entity.form.SocketMsgAddForm;
import org.monkey.example.websocket.entity.form.SocketMsgListForm;
import org.monkey.example.websocket.entity.form.SocketMsgPageForm;
import org.monkey.example.websocket.entity.form.SocketMsgSaveForm;
import org.monkey.example.websocket.entity.form.SocketMsgUpdateForm;
import org.monkey.example.websocket.entity.vo.SocketMsgVO;
import org.monkey.example.websocket.service.SocketMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@Data
@Api(value = "socket消息", tags = "socket消息")
@RestController
@ResponseBody
@RequestMapping("/v1/socketMsg")
public class SocketMsgController {


  @Autowired
  SocketMsgService socketMsgService;
  @Autowired
  SocketMsgConvert socketMsgConvert;
  @ApiOperation(value = "id查询", notes = "id查询")
  @GetMapping
  public Result<SocketMsgVO> get(@NotNull(message = "id必填") Long id,@RequestParam(required = false) Boolean nullThrow) {
    SocketMsgDTO  dto = socketMsgService.getById(id,nullThrow);
    return Result.success(socketMsgConvert.dto2Vo(dto));
  }
  @ApiOperation(value = "删除", notes = "删除")
  @PostMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
  public Result<Boolean> delete(@NotNull(message = "id必填") Long id) {
    return Result.success(socketMsgService.delete(id));
  }
  @ApiOperation(value = "添加", notes = "添加")
  @PostMapping(value = "/add",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
  public Result<SocketMsgVO> add(@RequestBody @Valid SocketMsgAddForm addForm) {
    SocketMsgDTO  dto = socketMsgService.add(addForm);
    return Result.success(socketMsgConvert.dto2Vo(dto));
  }
  @ApiOperation(value = "更新", notes = "更新")
  @PostMapping("/update")
  public Result<SocketMsgVO> update(@RequestBody  @Valid SocketMsgUpdateForm updateForm) {
    SocketMsgDTO  dto = socketMsgService.update(updateForm);
    return Result.success(socketMsgConvert.dto2Vo(dto));
  }
  @ApiOperation(value = "添加或更新", notes = "添加或更新")
  @PostMapping("/save")
  public Result<SocketMsgVO> save(@RequestBody  @Valid SocketMsgSaveForm saveForm) {
    SocketMsgDTO  dto = socketMsgService.save(saveForm);
    return Result.success(socketMsgConvert.dto2Vo(dto));
  }
  @ApiOperation(value = "获取所有", notes = "获取所有")
  @GetMapping("/listAll")
  public Result<List<SocketMsgVO>> listAll() {
    List<SocketMsgDTO> dts = socketMsgService.listAll();
    return Result.success(DataUtil.colTypeChange(dts,dto->socketMsgConvert.dto2Vo(dto)));
  }
  @ApiOperation(value = "分页条件查询", notes = "分页条件查询")
  @PostMapping(value = "/page",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
  public Result<IPage<SocketMsgVO>> page(@RequestBody  @Valid SocketMsgPageForm pageForm){
    IPage<SocketMsgDTO> generateDtPage = socketMsgService.page(pageForm);
    return Result.success(PageConvertUtil.convert(generateDtPage,g->socketMsgConvert.dto2Vo(g)));
  }
  @ApiOperation(value = "条件查询", notes = "条件查询")
  @PostMapping(value = "/list",produces = MediaType.APPLICATION_JSON_VALUE,consumes = MediaType.APPLICATION_JSON_VALUE)
  public Result<List<SocketMsgVO>> list(@RequestBody  @Valid SocketMsgListForm listForm){
    List<SocketMsgDTO> dts = socketMsgService.list(listForm);
    return Result.success(dts.stream().map(g->socketMsgConvert.dto2Vo(g)).collect(Collectors.toList()));
  }
}
